package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 题目分类Controller
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * TODO：重构日志和DTO转BO
     * 把DTO对象转化为BO对象 并进行日志参数校验
     *
     * @param method             日志方法名
     * @param subjectCategoryDTO DTO对象
     * @return BO对象
     */
    @SafeVarargs
    private static <T> SubjectCategoryBO getSubjectCategoryBO(String method, SubjectCategoryDTO subjectCategoryDTO, Map<T, String>... args) {
        // TODO:日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.{}.dto:{}", method, JSON.toJSONString(subjectCategoryDTO));
        }
        // 参数校验
        for (Map<T, String> arg : args) {
            arg.forEach(Preconditions::checkNotNull);
        }
        // 先将DTO转换为BO
        return SubjectCategoryDTOConverter.INSTANCE.converBoToCategoryDTO(subjectCategoryDTO);
    }

    /**
     * 新增题目分类
     *
     * @param subjectCategoryDTO 题目分类DTO
     * @return 是否新增成功
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            // TODO:参数校验
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类id不能为空");


            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                    .converBoToCategoryDTO(subjectCategoryDTO);

            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("subjectCategoryController.add.error {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询岗位大类 一级分类
     *
     * @return 查询结果
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = getSubjectCategoryBO("queryPrimaryCategory", subjectCategoryDTO, new HashMap<>(0));
            // 调用domainService查询一级分类
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            // 将查询结果转换为DTO对象
            List<SubjectCategoryDTO> dtoList = SubjectCategoryDTOConverter.INSTANCE.converBoToCategoryDTOList(subjectCategoryBOList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("subjectCategoryController.queryPrimaryCategory.error {}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询岗位大类根据parentID 一级分类
     *
     * @param subjectCategoryDTO 查询条件 DTO层
     * @return 查询结果
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Map<Long, String> stringMap = new HashMap<>(0);
            stringMap.put(subjectCategoryDTO.getParentId(), "父类id不能为空");
            SubjectCategoryBO subjectCategoryBO = getSubjectCategoryBO("queryCategoryByPrimary", subjectCategoryDTO, stringMap);
            // 调用domainService查询
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            // 将查询结果BO转换为DTO对象
            List<SubjectCategoryDTO> dto = SubjectCategoryDTOConverter.INSTANCE.converBoListToCategoryDTO(boList);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("subjectCategoryController.queryByPrimary.error {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新分类
     *
     * @param subjectCategoryDTO 根据查询条件
     * @return 是否成功
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Map<Long, String> stringMap = new HashMap<>(0);
            stringMap.put(subjectCategoryDTO.getId(), "id不能为空");
            SubjectCategoryBO subjectCategoryBO = getSubjectCategoryBO("update", subjectCategoryDTO, stringMap);
            // 调用domainService更新
            Boolean flag = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("subjectCategoryController.update.error {}", e.getMessage(), e);
            return Result.fail("修改失败");
        }
    }

    /**
     * 删除分类
     *
     * @param subjectCategoryDTO 根据查询条件
     * @return 是否成功
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Map<Long, String> stringMap = new HashMap<>(0);
            stringMap.put(subjectCategoryDTO.getId(), "id不能为空");
            SubjectCategoryBO subjectCategoryBO = getSubjectCategoryBO("delete", subjectCategoryDTO, stringMap);
            // 调用domainService更新
            Boolean flag = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("subjectCategoryController.delete.error {}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 查询分类及标签一次性
     *
     * @param subjectCategoryDTO 查询条件 DTO层
     * @return 查询结果
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Map<Long, String> stringMap = new HashMap<>(0);
            stringMap.put(subjectCategoryDTO.getParentId(), "父类id不能为空");
            SubjectCategoryBO subjectCategoryBO = getSubjectCategoryBO("queryCategoryAndLabel", subjectCategoryDTO, stringMap);
            // 调用domainService查询
            List<SubjectCategoryBO> boList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            List<SubjectCategoryDTO> dtoList = new LinkedList<>();
            boList.forEach(bo -> {
                SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.converBoToCategoryDTO(bo);
                List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConverter.INSTANCE.converBoListToLabelDTO(bo.getSubjectLabelBOList());
                dto.setLabelDTOList(labelDTOList);
                dtoList.add(dto);
            });

            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("subjectCategoryController.queryCategoryAndLabel.error {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
